package org.ctstudio.oa.duty.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.ctstudio.oa.duty.bean.Staff;
import org.ctstudio.oa.duty.bean.UidBean;
import org.ctstudio.oa.duty.bean.conf.Setting;
import org.ctstudio.oa.duty.dao.SettingMapper;
import org.ctstudio.oa.duty.dao.StaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

public abstract class AbstractController {
  @Autowired
  protected SettingMapper settingMapper;
  @Autowired
  protected StaffMapper staffMapper;

  @ModelAttribute("userName")
  public String getUserName(HttpServletRequest request) {
    return request.getRemoteUser();
  }

  protected Map<Long, String> getSettings(String prefix) {
    List<Setting> list = settingMapper.getByPrefix(prefix);
    return list.stream().collect(Collectors.toMap(Setting::getSortNo, Setting::getVal));
  }

  protected Map<String, String> getStaffIdNameMap(List<? extends UidBean> list) {
    if (list == null || list.isEmpty()) {
      return new HashMap<>();
    }
    List<String> uidList = list.stream().map(UidBean::getUid).distinct().collect(Collectors.toList());
    List<Staff> staffList = staffMapper.getByStaffIds(uidList);
    return staffList.stream().collect(Collectors.toMap(Staff::getStaffId, Staff::getName));
  }
}